home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter4 / procs.c < prev    next >
C/C++ Source or Header  |  1996-03-21  |  11KB  |  333 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "procs.h"
  5. #include "sqlext.h"  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "SQLProcedures()"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24. VOID DoSQLProcedureColumns( HWND hList, LPSTR szProcedureName );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. static HDBC  hDBC  = NULL;
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static HENV  hEnv  = NULL;
  112. static HWND  hList = NULL;
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE :
  117.               {
  118.                  RETCODE retCode;
  119.  
  120.                  // Allocate Environment and Connection.
  121.                  //.....................................
  122.                  SQLAllocEnv( &hEnv );
  123.                  SQLAllocConnect( hEnv, &hDBC );
  124.  
  125.                  // Connect to the data source
  126.                  //...........................
  127.                  retCode = SQLConnect( hDBC, 
  128.                              "Test Data Source", SQL_NTS,
  129.                              "DBA",              SQL_NTS,  
  130.                              "SQL",              SQL_NTS );
  131.  
  132.                  if ( retCode == SQL_SUCCESS )
  133.                  {
  134.                     int nTabSize = 60;
  135.  
  136.                     hList = CreateWindow( "LISTBOX", "",    
  137.                                LBS_NOTIFY | LBS_USETABSTOPS |
  138.                                LBS_NOINTEGRALHEIGHT  | 
  139.                                WS_BORDER  | WS_CHILD | 
  140.                                WS_VISIBLE | WS_VSCROLL, 
  141.                                0, 0, 0, 0, 
  142.                                hWnd, (HMENU)101,
  143.                                hInst, NULL );
  144.                     
  145.                     SendMessage( hList, LB_SETTABSTOPS, 
  146.                                  1, (LPARAM)&nTabSize );
  147.                  }
  148.               }
  149.               break;
  150.  
  151.       case WM_SIZE :
  152.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  153.                                        HIWORD( lParam ), TRUE );
  154.               break; 
  155.               
  156.       case WM_COMMAND :
  157.               switch( LOWORD( wParam ) )
  158.               {
  159.                  case IDM_TEST :
  160.                  {
  161.                     HSTMT   hStmt;
  162.                     UCHAR   szProcedureName[128];
  163.                     UCHAR   szLBStr[128];
  164.                     SDWORD  dwTypeLen;
  165.                     RETCODE retCode;
  166.  
  167.                     // Allocate a statement handle
  168.                     // to receive the result set.
  169.                     // ...........................
  170.                     SQLAllocStmt( hDBC, &hStmt );
  171.  
  172.                     // Call SQLProcedures() to determine all
  173.                     // procedures within the data source that
  174.                     // have a name that begins with 
  175.                     // 'sp_customer'.
  176.                     // ......................................
  177.                     retCode = SQLProcedures( hStmt, 
  178.                                              NULL, SQL_NTS, 
  179.                                              NULL, SQL_NTS, 
  180.                                    "sp_customer%", SQL_NTS ); 
  181.  
  182.                     if( retCode == SQL_SUCCESS || 
  183.                         retCode == SQL_SUCCESS_WITH_INFO )
  184.                     {
  185.                        // Bind a column to the PROCEDURE_NAME column in 
  186.                        // the result set generated from SQLProcedures().
  187.                        // ..............................................
  188.                        SQLBindCol( hStmt, 3, SQL_C_CHAR, 
  189.                           szProcedureName, sizeof( szProcedureName ), 
  190.                           &dwTypeLen );
  191.  
  192.                        // Fetch the records and display 
  193.                        // them in the list box.
  194.                        //..............................
  195.                        retCode = SQLFetch( hStmt );
  196.  
  197.                        while ( retCode == SQL_SUCCESS )
  198.                        {
  199.                           // Set string contents to be procedure name.
  200.                           // .........................................
  201.                           sprintf( szLBStr, "Procedure Name: %s", 
  202.                                              szProcedureName );
  203.  
  204.                           SendMessage( hList, LB_ADDSTRING, 
  205.                                        0, (LPARAM)szLBStr );
  206.  
  207.                           retCode = SQLFetch( hStmt );
  208.  
  209.                           // Call SQLProcedureColumns() to determine 
  210.                           // all procedure columns within the 
  211.                           // procedure returned from SQLProcedures().
  212.                           // ........................................
  213.                           DoSQLProcedureColumns( hList, 
  214.                                                  szProcedureName );
  215.                        }
  216.                     }
  217.  
  218.                     SQLFreeStmt( hStmt, SQL_DROP );
  219.                  }
  220.                  break;
  221.  
  222.                  case IDM_ABOUT :
  223.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  224.                         break;
  225.  
  226.                  case IDM_EXIT :
  227.                         DestroyWindow( hWnd );
  228.                         break;
  229.               }
  230.               break;
  231.       
  232.       case WM_DESTROY :
  233.               PostQuitMessage(0);
  234.  
  235.               // Disconnect Environment.
  236.               //........................
  237.               SQLDisconnect( hDBC );
  238.  
  239.               // Free Connection and Environment.
  240.               //.................................
  241.               SQLFreeConnect( hDBC );
  242.               SQLFreeEnv( hEnv );
  243.               break;
  244.  
  245.       default :
  246.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  247.    }
  248.  
  249.    return( 0L );
  250. }
  251.  
  252.  
  253. VOID DoSQLProcedureColumns( HWND hList, LPSTR szProcedureName )
  254. {
  255.    HSTMT   hStmt;
  256.    UCHAR   szColumnName[128];
  257.    UCHAR   szLBStr     [128];
  258.    SDWORD  dwTypeLen;
  259.    RETCODE retCode;
  260.    SHORT   nColNum;
  261.  
  262.    // Allocate a statement handle to receive the result set.
  263.    // ......................................................
  264.    SQLAllocStmt( hDBC, &hStmt );
  265.  
  266.    // Call SQLProcedureColumns() to determine 
  267.    // all procedure columns that exist within 
  268.    // procedures inside the data source.
  269.    // .......................................
  270.    retCode = SQLProcedureColumns( hStmt, 
  271.                                   NULL, SQL_NTS, 
  272.                                   NULL, SQL_NTS, 
  273.                        szProcedureName, SQL_NTS,
  274.                                   "%",  SQL_NTS ); 
  275.  
  276.    if( retCode == SQL_SUCCESS || 
  277.        retCode == SQL_SUCCESS_WITH_INFO )
  278.    {
  279.       // Bind a column to the COLUMN_NAME column in the
  280.       // result set generated from SQLStatistics().
  281.       // ..............................................
  282.       SQLBindCol( hStmt, 4, SQL_C_CHAR, 
  283.          szColumnName, sizeof( szColumnName ), 
  284.          &dwTypeLen );
  285.  
  286.       // Fetch all the records and
  287.       // display them in the list box.
  288.       //..............................
  289.       retCode = SQLFetch( hStmt );
  290.  
  291.       nColNum = 1;
  292.       while ( retCode == SQL_SUCCESS )
  293.       {
  294.          // Set string contents to be the returned column names.
  295.          // ...............................
  296.          sprintf( szLBStr, "   Column %02d: %s", 
  297.                            nColNum, szColumnName );
  298.  
  299.          SendMessage( hList, LB_ADDSTRING, 
  300.                       0, (LPARAM)szLBStr );
  301.  
  302.          retCode = SQLFetch( hStmt );
  303.          nColNum++;
  304.       }
  305.    }
  306.  
  307.    SQLFreeStmt( hStmt, SQL_DROP );
  308. }
  309.  
  310.  
  311. LRESULT CALLBACK About( HWND hDlg,           
  312.                         UINT message,        
  313.                         WPARAM wParam,       
  314.                         LPARAM lParam)
  315. {
  316.    switch (message) 
  317.    {
  318.        case WM_INITDIALOG: 
  319.                return (TRUE);
  320.  
  321.        case WM_COMMAND:                              
  322.                if (   LOWORD(wParam) == IDOK         
  323.                    || LOWORD(wParam) == IDCANCEL)    
  324.                {
  325.                        EndDialog(hDlg, TRUE);        
  326.                        return (TRUE);
  327.                }
  328.                break;
  329.    }
  330.  
  331.    return (FALSE); 
  332. }
  333.